// 回溯递归

let counter = 0;
let arr = [];
for (let i = 0; i < 8; i++){
    arr[i] = [];
    for (let j = 0; j < 8; j++){
        arr[i][j] = 0;
    }
}

function printQueenMap() { 
    console.log("---------------", counter);
    for (let i = 0; i < 8; i++){
        let str = "";
        for (let j = 0; j < 8; j++){
            str += " " + arr[i][j];
        }
        console.log(str);
    }
}

function check(i, j) { 
    for (let t = 0; t < 8; t++){
        if (arr[t][j] == 1 || arr[i][t] == 1) {
            return false;
        }
    }
    // left top
    for (let ii = i, jj = j; ii >= 0 && jj >= 0; ii-- , jj--){
        if (arr[ii][jj] == 1) {
            return false;
        }
    }
    // right top
    for (let ii = i, jj = j; ii >= 0 && jj < 8; ii-- , jj++){
        if (arr[ii][jj] == 1) {
            return false;
        }
    }
    return true;
}

function findQueen(i) {
    if (i > 7) {
        counter++;
        printQueenMap();
        // 1.all
        //return;
        // 2.just one
        return true;
    }

    for (let j = 0; j < 8; j++){
        if (check(i, j)) {
            arr[i][j] = 1;
            // 1.all
            //findQueen(i + 1);
            //arr[i][j] = 0;
            // 2.just one
            if (findQueen(i + 1)) {
                return true;
            } else {
                arr[i][j] = 0;
            }
        }
    }
    // 1.all
    //return;
    // 2.just one
    return false;
}

findQueen(0);